package com.smm.web.vmc.util;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by zhenghao on 2016/10/11.
 *
 * 极光推送操作工具类
 */
@Slf4j
public class JPushUtil {

    //极光平台密钥
    private static final String masterSecret="41a612ce2cf6b9fa427f6fd2";

    //在极光上注册的appkey
    private static final String appKey = "0dca27b83d747cc09f5617e9";

    private JPushClient jpushClient;

    public JPushUtil() {

        jpushClient = new JPushClient(masterSecret, appKey);
    }

    /**
     * 发送消息给所有用户
     * @param content
     * @throws Exception
     */
    public void sendAll(String content) throws Exception{

        PushPayload payload = PushPayload.alertAll(content);

        PushResult result = jpushClient.sendPush(payload);

        log.info("---推送消息成功。"+ result);

    }


    /**
     * 发送消息给指定用户
     * @param content
     * @param userId
     * @throws Exception
     */
    public void sendTo(String content ,int userId) throws Exception{

        String target = getTargetByUserId(userId);

        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.tag(target))
                .setNotification(Notification.alert(content))
                .build();

        PushResult result = jpushClient.sendPush(payload);

        log.info("---推送消息成功。"+ result);
        
        

    }

    private String getTargetByUserId(int userId) {

        return "tag1";
    }


}
